﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using log4net;
using System.IO;
using CodeCamp12.Domain;

namespace CodeCamp12.DAL
{
    public class AdditionalDataManagement
    {
        private  ConnectionStringProvider connString = new ConnectionStringProvider();
        private ILog log = LogManager.GetLogger("CodeCamp12.DAL");

        /// <summary>
        /// Updates contributors html page
        /// </summary>
        /// <param name="link"></param>
        public void UpdateContributors(string link)
        {
            var comm = new SqlCommand("UpdateContributors") { CommandType = System.Data.CommandType.StoredProcedure };
            comm.Parameters.AddWithValue("@link", link);
            UpdateAdditional(comm);
        }

        /// <summary>
        /// Updates the facebook icon link
        /// </summary>
        /// <param name="link"></param>
        public void UpdateFacebook(string link)
        {
            var comm = new SqlCommand("UpdateFacebookLink") { CommandType = System.Data.CommandType.StoredProcedure };
            comm.Parameters.AddWithValue("@link", link);
            UpdateAdditional(comm);
        }

        /// <summary>
        /// Updates the twitter icon link
        /// </summary>
        /// <param name="link"></param>
        public void UpdateTwitter(string link)
        {
            var comm = new SqlCommand("UpdateTwitterLink") { CommandType = System.Data.CommandType.StoredProcedure };
            comm.Parameters.AddWithValue("@link", link);
            UpdateAdditional(comm);
        }

        /// <summary>
        /// Updates the Linkedin icon link
        /// </summary>
        /// <param name="link"></param>
        public void UpdateLinkedinLink(string link)
        {
            var comm = new SqlCommand("UpdateLinkedinLink") { CommandType = System.Data.CommandType.StoredProcedure };
            comm.Parameters.AddWithValue("@link", link);
            UpdateAdditional(comm);
        }

        /// <summary>
        /// Updates the homepage video
        /// </summary>
        /// <param name="link"></param>
        public void UpdateYouTubeVideo(string link)
        {
            var comm = new SqlCommand("UpdateYouTubeLink") { CommandType = System.Data.CommandType.StoredProcedure };
            comm.Parameters.AddWithValue("@link", link);
            UpdateAdditional(comm);
        }

        /// <summary>
        /// Updates the top page logo image
        /// </summary>
        /// <param name="image"></param>
        public void UpdateLogo(Stream image)
        {
            var comm = new SqlCommand("UpdateLogo") { CommandType = System.Data.CommandType.StoredProcedure };
            var documentBinary = new byte[image.Length];
            image.Read(documentBinary, 0, (int) image.Length);
            comm.Parameters.AddWithValue("@logo", documentBinary);
            UpdateAdditional(comm);
        }

        /// <summary>
        /// Gets all master page elements
        /// </summary>
        /// <returns></returns>
        public OtherProperties GetAdditionalStuff()
        {
            var conn = connString.GetConnection();
            var comm = new SqlCommand("GetAdditional", conn) { CommandType = System.Data.CommandType.StoredProcedure };
            var otherObj = new OtherProperties();
            try
            {
                conn.Open();
                var reader = comm.ExecuteReader();
                if(reader.Read())
                {
                    otherObj.Contributors = reader["Contributors"].ToString();
                    otherObj.Facebook = reader["Facebook"].ToString();
                    otherObj.Twitter = reader["Twitter"].ToString();
                    otherObj.Linkedin = reader["Linkedn"].ToString();
                    otherObj.EmbededVideo = reader["EmbedYouTube"].ToString();

                }
                reader.Close();
            }
            catch (Exception ex)
            {
               
                log.Error(ex);
                throw;
            }
            return otherObj;
        }
        
        private void UpdateAdditional(SqlCommand comm)
        {
            var conn = connString.GetConnection();
            comm.Connection = conn;

            try
            {
                conn.Open();
                comm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw;
            }
            finally
            {
                conn.Close();
            }
        }

        /// <summary>
        /// Gets the logo image
        /// </summary>
        /// <returns></returns>
        public Stream GetLogo()
        {
            Stream result = null;
            var conn = connString.GetConnection();
            var comm = new SqlCommand("GetLogo", conn) { CommandType = System.Data.CommandType.StoredProcedure };

            try
            {
                conn.Open();
                SqlDataReader reader = comm.ExecuteReader();
                if (reader.Read())
                {
                    result = new MemoryStream((byte[])reader["Logo"]); 
                }
                reader.Close();

            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw;
            }
            finally
            {
                conn.Close();
            }

            return result;
        }
        

    }
}
